package com.example.zg3_park.CarType

import android.view.ViewGroup
import androidx.paging.PagingDataAdapter
import androidx.paging.PagingSource
import androidx.paging.PagingState
import com.example.lib_base.entity.CarTypeEntity
import com.example.lib_base.entity.CarTypeEntityItem
import com.example.lib_base.entity.Res
import com.example.lib_base.net.ApiService

//apiService
/**
 * para:请求网络的参数值  （车位、类型之类额）
 * type:用于判断请求的哪个接口
 */
//,val para:Any,val type:String
class CarTypeDataSource(val apiService: ApiService,val para:String,val type:String):PagingSource<Int,CarTypeEntityItem>() {
    override fun getRefreshKey(state: PagingState<Int, CarTypeEntityItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, CarTypeEntityItem> {
        try {
            //网络
            //全局变量
            var carType : Res<CarTypeEntity>?=null
            if(type == "类型") {
                carType = apiService.getCarType(para)
            }else  if(type == "状态") {
                carType = apiService.getstatus(para)
            }else if(type == "区域") {
                //carType = apiService.getID(para.toString().toInt())
                carType = apiService.getId(para.toInt())
            }

            return LoadResult.Page(carType?.data!!,null,null)
        }catch (e:Exception) {
            return LoadResult.Error(e)
        }
    }

}